<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Model;

/**
 * Description of BookShelfModel
 *
 * @author gangguo
 */
class BookTjModel extends XuecheModel{

    protected $_useCache = true;

    protected $tableName = 'book_tj';
    protected $fields = array(
        'book_id','member_id','ctime','year','month','day','article_id'
    );


    public function Validate($data) {
        if(!is_array($data) || empty($data)){
            return FALSE;
        }
        foreach ($data as $key => $value) {
            # code...
            if($key == 'article_id'){
                continue;
            }
            if(empty($value) || !is_numeric($value)){
                return FALSE;
            }
        }
        return true;
    }

    //推荐投票记录
    public function savetj($data){
        if(!$this->Validate($data)){
            return FALSE;
        }
        return $this->add($data);
    }

    //获取当天推荐投票的次数
    public function getTjNums($member_id){
        if($member_id > 0){
            $year = date('Y',time());
            $month = date('m',time());
            $day = date('d',time());
            $where = "`member_id`='{$member_id}' AND `year`='{$year}' AND `month`='{$month}' AND `day`='{$day}'";
            return $this->where($where)->count('book_id');
        }
    }

    //每本书，每天，每个人只能推荐一次
    public function checkOneTj($data){
        if(!$this->Validate($data)){
            return FALSE;
        }
        $where = "`article_id`='{$data['article_id']}' AND `book_id`='{$data['book_id']}' AND `member_id`='{$data['member_id']}' AND `year`='{$data['year']}' AND `month`='{$data['month']}' AND `day`='{$data['day']}'";
        if($this->where($where)->find()){
            return FALSE;
        }
        return true;
    }

    //周推荐
    public function weekTj($cate_id = 0,$limit = 15){
        $cacheName = md5(__METHOD__.$cate_id.$limit);
        $cacheValue = S($cacheName);
        if($this->_useCache && $cacheValue){
            return $cacheValue;
        }
        $time = time() - 604800;
        if($cate_id > 0){
            $sql = "SELECT count(tj.book_id) as tjNum,tj.book_id FROM `sdb_book_tj` as tj,`sdb_book` as b WHERE (b.cate_id = '{$cate_id}' AND b.book_id=tj.book_id AND tj.ctime > '{$time}') GROUP BY book_id ORDER BY tjNum DESC LIMIT {$limit}";
        }else{
            $sql = "SELECT count(tj.book_id) as tjNum,tj.book_id FROM `sdb_book_tj` as tj WHERE (`ctime` > '{$time}') GROUP BY book_id ORDER BY tjNum DESC LIMIT {$limit}";
        }
        $data = $this->query($sql);
        if($this->_useCache){
            S($cacheName,$data);
        }
        return $data;
    }

    //月推荐
    public function monthTj($cate_id = 0,$limit = 15){
        $cacheName = md5(__METHOD__.$cate_id.$limit);
        $cacheValue = S($cacheName);
        if($this->_useCache && $cacheValue){
            return $cacheValue;
        }

        $month = date('m',time());
        if($cate_id > 0){
            $sql = "SELECT count(tj.book_id) as tjNum,tj.book_id FROM `sdb_book_tj` as tj,`sdb_book` as b WHERE (b.cate_id = '{$cate_id}' AND b.book_id=tj.book_id AND tj.month = '{$month}') GROUP BY book_id ORDER BY tjNum DESC LIMIT {$limit}";
        }else{
            $sql = "SELECT count(tj.book_id) as tjNum,tj.book_id FROM `sdb_book_tj` as tj WHERE (`month` = '{$month}') GROUP BY book_id ORDER BY tjNum DESC LIMIT {$limit}";
        }
        $data = $this->query($sql);
        if($this->_useCache){
            S($cacheName,$data);
        }
        return $data;
    }

    //总推荐
    public function totalTj($cate_id = 0,$limit = 15){
        $cacheName = md5(__METHOD__.$cate_id.$limit);
        $cacheValue = S($cacheName);
        if($this->_useCache && $cacheValue){
            return $cacheValue;
        }
        if($cate_id > 0){
            $sql = "SELECT *,tj_num as tjNum FROM `sdb_book` WHERE (cate_id = '{$cate_id}') ORDER BY tj_num DESC LIMIT {$limit}";
        }else{
            $sql = "SELECT *,tj_num as tjNum FROM `sdb_book` ORDER BY tj_num DESC LIMIT {$limit}";
        }
        $data = $this->query($sql);
        if($this->_useCache){
            S($cacheName,$data);
        }
        return $data;

    }








//put your code here
}
